﻿using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.BusinessLogicLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CuaHangMayTinh.PresentationLayer.Report
{
    /// <summary>
    /// Enum report hóa đơn
    /// </summary>
    public enum ReportHoaDon
    {
        NhapHang,
        BanHang,
        XuatKhoTraHang,
        BaoHanhSuaChua
    };
    /// <summary>
    /// Form Hiển thị thông tin hóa đơn và danh sách các mặt hàng kèm theo
    /// </summary>
    public partial class FormReportHoaDon : Form
    {
        ReportHoaDon rh = ReportHoaDon.NhapHang;

        List<BanHangEntity> listBanhang = new List<BanHangEntity>();

        List<NhapHangEntity> listNhapHang = new List<NhapHangEntity>();

        List<XuatKhoTraHangEntity> listXuatkho = new List<XuatKhoTraHangEntity>();

        List<BaoHanhSuaChuaEntity> listbaohanh = new List<BaoHanhSuaChuaEntity>();

        List<NhanVienEntity> listNhanVien = new List<NhanVienEntity>();

        List<KhachHangEntity> listKhachHang = new List<KhachHangEntity>();

        List<NhaCungCapEntity> listNhaCungCap = new List<NhaCungCapEntity>();

        List<DonViTinhEntity> listdonvitinh = new List<DonViTinhEntity>();

        List<MatHangEntity> listmathang = new List<MatHangEntity>();

        List<ModelMatHangEntity> listmodelmathang = new List<ModelMatHangEntity>();

        List<InfoEntity> listinfo = new List<InfoEntity>();

        public FormReportHoaDon()
        {
            listinfo = InfoManager.ConvertToList(InfoManager.GetThongTin());
            InitializeComponent();
        }

        /// <summary>
        /// Khởi tạo với đầu vào là hóa đơn nhập hàng
        /// </summary>
        /// <param name="nhaphang"></param>
        /// <param name="nhanvien"></param>
        /// <param name="ncc"></param>
        /// <param name="listdonvitinh"></param>
        public FormReportHoaDon(NhapHangEntity nhaphang, NhanVienEntity nhanvien, NhaCungCapEntity ncc, List<DonViTinhEntity> listdonvitinh, bool Print)
            : this()
        {
            rh = ReportHoaDon.NhapHang;
            listNhapHang.Add(nhaphang);
            listNhanVien.Add(nhanvien);
            listNhaCungCap.Add(ncc);
            foreach (var model in nhaphang.ListModel)
            {
                foreach (var mathang in model.ListMatHang)
                {
                    mathang.MaModelMH = model.MaModelMH;
                    listmathang.Add(mathang);
                }
            }
            this.listdonvitinh = listdonvitinh;
            reportViewer1.ShowPrintButton = Print;
        }

        /// <summary>
        /// Khởi tạo với đầu vào là hóa đơn bán hàng
        /// </summary>
        public FormReportHoaDon(BanHangEntity banhang, NhanVienEntity nhanvien, KhachHangEntity kh, List<DonViTinhEntity> listdonvitinh, bool Print)
            :this()
        {
            rh = ReportHoaDon.BanHang;
            listBanhang.Add(banhang);
            listNhanVien.Add(nhanvien);
            listKhachHang.Add(kh);
            foreach (var model in banhang.ListModel)
            {
                foreach (var mathang in model.ListMatHang)
                {
                    listmathang.Add(mathang);
                }
            }
            this.listdonvitinh = listdonvitinh;
            reportViewer1.ShowPrintButton = Print;
        }


        /// <summary>
        /// Khởi tạo với đầu vào là phiếu xuất kho
        /// </summary>
        public FormReportHoaDon(XuatKhoTraHangEntity xuatkho, NhanVienEntity nhanvien, NhaCungCapEntity ncc, List<DonViTinhEntity> listdonvitinh, bool Print):this()
        {
            rh = ReportHoaDon.XuatKhoTraHang;
            listXuatkho.Add(xuatkho);
            listNhanVien.Add(nhanvien);
            listNhaCungCap.Add(ncc);
            foreach (var model in xuatkho.ListModel)
            {
                foreach (var mathang in model.ListMatHang)
                {
                    listmathang.Add(mathang);
                }
            }
            this.listdonvitinh = listdonvitinh;
            reportViewer1.ShowPrintButton = Print;
        }

        /// <summary>
        /// Khởi tạo với đầu vào là phiếu bảo hành sửa chữa
        /// </summary>
        public FormReportHoaDon(BaoHanhSuaChuaEntity baohanh, List<ModelMatHangEntity> model, NhanVienEntity nhanvien, KhachHangEntity khachhang, bool Print):this()
        {
            rh = ReportHoaDon.BaoHanhSuaChua;

            listbaohanh.Add(baohanh);
            listNhanVien.Add(nhanvien);
            listKhachHang.Add(khachhang);
            listmathang.Add(baohanh.mathang);
            listmodelmathang = model;
            reportViewer1.ShowPrintButton = Print;
            btn_DsMatHang.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormReportHoaDon_Load(object sender, EventArgs e)
        {
            switch (rh)
            {
                case ReportHoaDon.NhapHang:
                    ReportInfoNhapHang();
                    this.Text = "Hóa đơn nhập hàng";
                    break;
                case ReportHoaDon.BanHang:
                    ReportInfoBanHang();
                    this.Text = "Hóa đơn bán hàng";
                    break;
                case ReportHoaDon.XuatKhoTraHang:
                    ReportInfoXuatKhoTraHang();
                    this.Text = "Phiếu xuất kho trả hàng";
                    break;
                case ReportHoaDon.BaoHanhSuaChua:
                    ReportInfoBHSC();
                    this.Text = "Phiếu bảo hành sửa chữa";
                    break;
                default:
                    this.reportViewer1.RefreshReport();
                    break;
            }
        }

        /// <summary>
        /// Hàm hiển thị hóa đơn lên reportViewer
        /// </summary>
        void ReportInfoBanHang()
        {
            reportViewer1.LocalReport.DataSources.Clear(); //clear report
            reportViewer1.LocalReport.ReportEmbeddedResource = "CuaHangMayTinh.PresentationLayer.Report.HoaDonBanHangReport.rdlc"; // bind reportviewer with .rdlc

            Microsoft.Reporting.WinForms.ReportDataSource banhangds = new Microsoft.Reporting.WinForms.ReportDataSource("BanHangDataSet", listBanhang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource nhanviends = new Microsoft.Reporting.WinForms.ReportDataSource("NhanVienDataSet", listNhanVien); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource modelds = new Microsoft.Reporting.WinForms.ReportDataSource("ModelMatHangDataSet", listBanhang[0].ListModel); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource chitietds = new Microsoft.Reporting.WinForms.ReportDataSource("ChiTietBanHangDataSet", listBanhang[0].ListChiTiet); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource khachangds = new Microsoft.Reporting.WinForms.ReportDataSource("KhachHangDataSet", listKhachHang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource dvtds = new Microsoft.Reporting.WinForms.ReportDataSource("DonViTinhDataSet", listdonvitinh); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource infods = new Microsoft.Reporting.WinForms.ReportDataSource("InfoDataSet", listinfo); // set the datasource
            reportViewer1.LocalReport.DataSources.Add(banhangds);
            reportViewer1.LocalReport.DataSources.Add(nhanviends);
            reportViewer1.LocalReport.DataSources.Add(modelds);
            reportViewer1.LocalReport.DataSources.Add(chitietds);
            reportViewer1.LocalReport.DataSources.Add(khachangds);
            reportViewer1.LocalReport.DataSources.Add(dvtds);
            reportViewer1.LocalReport.DataSources.Add(infods);

            banhangds.Value = listBanhang;
            nhanviends.Value = listNhanVien;
            modelds.Value = listBanhang[0].ListModel;
            chitietds.Value = listBanhang[0].ListChiTiet;
            khachangds.Value = listKhachHang;
            dvtds.Value = listdonvitinh;
            infods.Value = listinfo;

            reportViewer1.LocalReport.Refresh();
            reportViewer1.RefreshReport(); // refresh report
        }

        /// <summary>
        /// Hàm hiển thị danh sách các mặt hàng lên reportViewer
        /// </summary>
        void ReportDanhSachMHBan()
        {
            reportViewer1.LocalReport.DataSources.Clear(); //clear report
            reportViewer1.LocalReport.ReportEmbeddedResource = "CuaHangMayTinh.PresentationLayer.Report.HoaDonBanHangListMHReport.rdlc"; // bind reportviewer with .rdlc

            Microsoft.Reporting.WinForms.ReportDataSource banhangds = new Microsoft.Reporting.WinForms.ReportDataSource("BanHangDataSet", listBanhang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource modelds = new Microsoft.Reporting.WinForms.ReportDataSource("ModelMatHangDataSet", listBanhang[0].ListModel); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource mathangds = new Microsoft.Reporting.WinForms.ReportDataSource("MatHangDataSet", listmathang); // set the datasource
            

            reportViewer1.LocalReport.DataSources.Add(banhangds);
            reportViewer1.LocalReport.DataSources.Add(modelds);
            reportViewer1.LocalReport.DataSources.Add(mathangds);
            

            banhangds.Value = listBanhang;
            modelds.Value = listBanhang[0].ListModel;
            mathangds.Value = listmathang;
            

            reportViewer1.LocalReport.Refresh();
            reportViewer1.RefreshReport(); // refresh report
        }

        /// <summary>
        /// Hàm hiển thị hóa đơn nhập lên reportViewer
        /// </summary>
        void ReportInfoNhapHang()
        {
            reportViewer1.LocalReport.DataSources.Clear(); //clear report
            reportViewer1.LocalReport.ReportEmbeddedResource = "CuaHangMayTinh.PresentationLayer.Report.HoaDonNhapHangReport.rdlc"; // bind reportviewer with .rdlc

            Microsoft.Reporting.WinForms.ReportDataSource nhaphangds = new Microsoft.Reporting.WinForms.ReportDataSource("NhapHangDataSet", listNhapHang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource nhanviends = new Microsoft.Reporting.WinForms.ReportDataSource("NhanVienDataSet", listNhanVien); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource modelds = new Microsoft.Reporting.WinForms.ReportDataSource("ModelMatHangDataSet", listNhapHang[0].ListModel); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource chitietds = new Microsoft.Reporting.WinForms.ReportDataSource("ChiTietNhapHangDataSet", listNhapHang[0].ListChiTietNhap); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource nhacungcapds = new Microsoft.Reporting.WinForms.ReportDataSource("NhaCungCapDataSet", listNhaCungCap); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource dvtds = new Microsoft.Reporting.WinForms.ReportDataSource("DonViTinhDataSet", listdonvitinh); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource infods = new Microsoft.Reporting.WinForms.ReportDataSource("InfoDataSet", listinfo); // set the datasource

            reportViewer1.LocalReport.DataSources.Add(nhaphangds);
            reportViewer1.LocalReport.DataSources.Add(nhanviends);
            reportViewer1.LocalReport.DataSources.Add(modelds);
            reportViewer1.LocalReport.DataSources.Add(chitietds);
            reportViewer1.LocalReport.DataSources.Add(nhacungcapds);
            reportViewer1.LocalReport.DataSources.Add(dvtds);
            reportViewer1.LocalReport.DataSources.Add(infods);

            nhaphangds.Value = listNhapHang;
            nhanviends.Value = listNhanVien;
            modelds.Value = listNhapHang[0].ListModel;
            chitietds.Value = listNhapHang[0].ListChiTietNhap;
            nhacungcapds.Value = listNhaCungCap;
            dvtds.Value = listdonvitinh;
            infods.Value = listinfo;

            reportViewer1.LocalReport.Refresh();
            reportViewer1.RefreshReport(); // refresh report
        }

        /// <summary>
        /// Hàm hiển thị danh sách các mặt hàng lên reportViewer
        /// </summary>
        void ReportDanhSachMHNhap()
        {
            reportViewer1.LocalReport.DataSources.Clear(); //clear report
            reportViewer1.LocalReport.ReportEmbeddedResource = "CuaHangMayTinh.PresentationLayer.Report.HoaDonBanHangListMHReport.rdlc"; // bind reportviewer with .rdlc

            Microsoft.Reporting.WinForms.ReportDataSource banhangds = new Microsoft.Reporting.WinForms.ReportDataSource("BanHangDataSet", listBanhang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource modelds = new Microsoft.Reporting.WinForms.ReportDataSource("ModelMatHangDataSet", listNhapHang[0].ListModel); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource mathangds = new Microsoft.Reporting.WinForms.ReportDataSource("MatHangDataSet", listmathang); // set the datasource

            reportViewer1.LocalReport.DataSources.Add(banhangds);
            reportViewer1.LocalReport.DataSources.Add(modelds);
            reportViewer1.LocalReport.DataSources.Add(mathangds);

            banhangds.Value = listBanhang;
            modelds.Value = listNhapHang[0].ListModel;
            mathangds.Value = listmathang;

            reportViewer1.LocalReport.Refresh();
            reportViewer1.RefreshReport(); // refresh report
        }

        /// <summary>
        /// Hàm hiển thị hóa đơn nhập lên reportViewer
        /// </summary>
        void ReportInfoXuatKhoTraHang()
        {
            reportViewer1.LocalReport.DataSources.Clear(); //clear report
            reportViewer1.LocalReport.ReportEmbeddedResource = "CuaHangMayTinh.PresentationLayer.Report.PhieuXuatKhoTraHangReport.rdlc"; // bind reportviewer with .rdlc

            Microsoft.Reporting.WinForms.ReportDataSource xuatkhods = new Microsoft.Reporting.WinForms.ReportDataSource("XuatKhoTraHangDataSet", listXuatkho); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource nhanviends = new Microsoft.Reporting.WinForms.ReportDataSource("NhanVienDataSet", listNhanVien); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource modelds = new Microsoft.Reporting.WinForms.ReportDataSource("ModelMatHangDataSet", listXuatkho[0].ListModel); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource chitietds = new Microsoft.Reporting.WinForms.ReportDataSource("ChiTietXuatKhoTraHangDataSet", listXuatkho[0].ListChiTiet); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource nhacungcapds = new Microsoft.Reporting.WinForms.ReportDataSource("NhaCungCapDataSet", listNhaCungCap); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource dvtds = new Microsoft.Reporting.WinForms.ReportDataSource("DonViTinhDataSet", listdonvitinh); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource infods = new Microsoft.Reporting.WinForms.ReportDataSource("InfoDataSet", listinfo); // set the datasource

            reportViewer1.LocalReport.DataSources.Add(xuatkhods);
            reportViewer1.LocalReport.DataSources.Add(nhanviends);
            reportViewer1.LocalReport.DataSources.Add(modelds);
            reportViewer1.LocalReport.DataSources.Add(chitietds);
            reportViewer1.LocalReport.DataSources.Add(nhacungcapds);
            reportViewer1.LocalReport.DataSources.Add(dvtds);
            reportViewer1.LocalReport.DataSources.Add(infods);

            xuatkhods.Value = listXuatkho;
            nhanviends.Value = listNhanVien;
            modelds.Value = listXuatkho[0].ListModel;
            chitietds.Value = listXuatkho[0].ListChiTiet;
            nhacungcapds.Value = listNhaCungCap;
            dvtds.Value = listdonvitinh;
            infods.Value = listinfo;

            reportViewer1.LocalReport.Refresh();
            reportViewer1.RefreshReport(); // refresh report
        }

        /// <summary>
        /// Hàm hiển thị danh sách các mặt hàng lên reportViewer
        /// </summary>
        void ReportDanhSachMHXuatKhoTraHang()
        {
            reportViewer1.LocalReport.DataSources.Clear(); //clear report
            reportViewer1.LocalReport.ReportEmbeddedResource = "CuaHangMayTinh.PresentationLayer.Report.HoaDonBanHangListMHReport.rdlc"; // bind reportviewer with .rdlc

            Microsoft.Reporting.WinForms.ReportDataSource banhangds = new Microsoft.Reporting.WinForms.ReportDataSource("BanHangDataSet", listBanhang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource modelds = new Microsoft.Reporting.WinForms.ReportDataSource("ModelMatHangDataSet", listXuatkho[0].ListModel); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource mathangds = new Microsoft.Reporting.WinForms.ReportDataSource("MatHangDataSet", listmathang); // set the datasource

            reportViewer1.LocalReport.DataSources.Add(banhangds);
            reportViewer1.LocalReport.DataSources.Add(modelds);
            reportViewer1.LocalReport.DataSources.Add(mathangds);

            banhangds.Value = listBanhang;
            modelds.Value = listXuatkho[0].ListModel;
            mathangds.Value = listmathang;

            reportViewer1.LocalReport.Refresh();
            reportViewer1.RefreshReport(); // refresh report
        }

        /// <summary>
        /// 
        /// </summary>
        void ReportInfoBHSC()
        {
            reportViewer1.LocalReport.DataSources.Clear(); //clear report
            reportViewer1.LocalReport.ReportEmbeddedResource = "CuaHangMayTinh.PresentationLayer.Report.PhieuBaoHanhSuaChuaReport.rdlc"; // bind reportviewer with .rdlc

            Microsoft.Reporting.WinForms.ReportDataSource baohanhds = new Microsoft.Reporting.WinForms.ReportDataSource("BaoHanhSuaChuaDataSet", listbaohanh); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource nhanviends = new Microsoft.Reporting.WinForms.ReportDataSource("NhanVienDataSet", listNhanVien); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource modelds = new Microsoft.Reporting.WinForms.ReportDataSource("ModelMatHangDataSet", listmodelmathang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource khachhangds = new Microsoft.Reporting.WinForms.ReportDataSource("KhachHangDataSet", listKhachHang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource mathangds = new Microsoft.Reporting.WinForms.ReportDataSource("MatHangDataSet", listmathang); // set the datasource
            Microsoft.Reporting.WinForms.ReportDataSource infods = new Microsoft.Reporting.WinForms.ReportDataSource("InfoDataSet", listinfo); // set the datasource

            reportViewer1.LocalReport.DataSources.Add(baohanhds);
            reportViewer1.LocalReport.DataSources.Add(nhanviends);
            reportViewer1.LocalReport.DataSources.Add(modelds);
            reportViewer1.LocalReport.DataSources.Add(khachhangds);
            reportViewer1.LocalReport.DataSources.Add(mathangds);
            reportViewer1.LocalReport.DataSources.Add(infods);

            baohanhds.Value = listbaohanh;
            nhanviends.Value = listNhanVien;
            modelds.Value = listmodelmathang;
            khachhangds.Value = listKhachHang;
            mathangds.Value = listmathang;
            infods.Value = listinfo;

            reportViewer1.LocalReport.Refresh();
            reportViewer1.RefreshReport(); // refresh report
        }

        /// <summary>
        /// xử lý sự kiện khi click vào button thông tin hóa đơn
        /// Hiển thị thông tin hóa đơn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_HoaDon_Click(object sender, EventArgs e)
        {
            switch (rh)
            {
                case ReportHoaDon.NhapHang:
                    ReportInfoNhapHang();
                    break;
                case ReportHoaDon.BanHang:
                    ReportInfoBanHang();
                    break;
                case ReportHoaDon.XuatKhoTraHang:
                    ReportInfoXuatKhoTraHang();
                    break;
                case ReportHoaDon.BaoHanhSuaChua:
                    ReportInfoBHSC();
                    break;
                default:
                    this.reportViewer1.RefreshReport();
                    break;
            }
        }

        /// <summary>
        /// xử lý sự kiện khi click vào button danh sách mặt hàng
        /// Hiển thị danh sách các mặt hàng thuộc hóa đơn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DsMatHang_Click(object sender, EventArgs e)
        {
            switch (rh)
            {
                case ReportHoaDon.NhapHang:
                    ReportDanhSachMHNhap();
                    break;
                case ReportHoaDon.BanHang:
                    ReportDanhSachMHBan();
                    break;
                case ReportHoaDon.XuatKhoTraHang:
                    ReportDanhSachMHXuatKhoTraHang();
                    break;
                default:
                    this.reportViewer1.RefreshReport();
                    break;
            }
        }
    }
}
